﻿define(["jquery", "mustache", "app/data-persister", "app/controls"], function ($, Mustache, data, controls) {
    function load(localhostUrl) {
        var studentTemplate = Mustache.compile(document.getElementById("student-template").innerHTML);
        var marksTemplate = Mustache.compile(document.getElementById("marks-template").innerHTML);

        $("#wrapper").on("click", "td.student", function () {
            data.marksFromStudentsId(localhostUrl, $(this).attr("id"))
                .then(function (marks) {
                    var markTableView = controls.getTableView(marks);

                    var marksHtml = markTableView.render(marksTemplate);
                    //console.log(marksHtml);

                    document.getElementById("content").innerHTML = marksHtml;
                });
        });

        $("#wrapper").on("click", "#getAllStudents", function () {
            data.allStudents(localhostUrl)
            .then(function (students) {

                var studentTableView = controls.getTableView(students);

                var studentHtml = studentTableView.render(studentTemplate, true);

                document.getElementById("content").innerHTML = studentHtml;

            }, function (err) {
                console.error(err);
            });
        });
    }
    
    return {
        load: load
    };
});